﻿using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;

namespace E.Story
{
    /// <summary>
    /// 添加节点对话框
    /// </summary>
    public class NodeCreationBox : ScriptableObject, ISearchWindowProvider
    {
        private StoryGraphView graphViewer;
        private Texture2D indentationIcon;
        private VisualElement visualElement;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="viewer">故事视图元素</param>
        public void Init(StoryGraphView viewer)
        {
            graphViewer = viewer;

            // 设置缩进图标
            indentationIcon = new Texture2D(1, 1);
            indentationIcon.SetPixel(0, 0, Color.clear);
            indentationIcon.Apply();
        }

        /// <summary>
        /// 传入额外参数
        /// </summary>
        /// <param name="ve">UI元素</param>
        public void SetVisualElement(VisualElement ve)
        {
            visualElement = ve;
        }

        /// <summary>
        /// 创建搜索树
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns>搜索树</returns>
        public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context)
        {
            List<SearchTreeEntry> searchTreeEntries = new()
            {
                new SearchTreeGroupEntry(new GUIContent("添加节点")),
                //new SearchTreeGroupEntry(new GUIContent("分组"),1),
                //new SearchTreeEntry(new GUIContent("分组", indentationIcon))
                //{
                //    level = 1,
                //    userData = new Group()
                //},
                
                //new SearchTreeEntry(new GUIContent("基础", indentationIcon))
                //{
                //    level = 1,
                //    userData = NodeType.Base
                //},
                ////new SearchTreeEntry(new GUIContent("零进零出", indentationIcon))
                ////{
                ////    level = 1,
                ////    userData = NodeType.ZeroInZeroOut
                ////},
                //new SearchTreeEntry(new GUIContent("零进单出", indentationIcon))
                //{
                //    level = 1,
                //    userData = NodeType.ZeroInSingleOut
                //},
                ////new SearchTreeEntry(new GUIContent("零进多出", indentationIcon))
                ////{
                ////    level = 1,
                ////    userData = NodeType.ZeroInMultiOut
                ////},
                //new SearchTreeEntry(new GUIContent("单进零出", indentationIcon))
                //{
                //    level = 1,
                //    userData = NodeType.SingleInZeroOut
                //},
                //new SearchTreeEntry(new GUIContent("单进单出", indentationIcon))
                //{
                //    level = 1,
                //    userData = NodeType.SingleInSingleOut
                //},
                //new SearchTreeEntry(new GUIContent("单进多出", indentationIcon))
                //{
                //    level = 1,
                //    userData = NodeType.SingleInMultiOut
                //},
                ////new SearchTreeEntry(new GUIContent("多进零出", indentationIcon))
                ////{
                ////    level = 1,
                ////    userData = NodeType.MultiInZeroOut
                ////},
                ////new SearchTreeEntry(new GUIContent("多进单出", indentationIcon))
                ////{
                ////    level = 1,
                ////    userData = NodeType.MultiInSingleOut
                ////},
                ////new SearchTreeEntry(new GUIContent("多进多出", indentationIcon))
                ////{
                ////    level = 1,
                ////    userData = NodeType.MultiInMultiOut
                ////},

                new SearchTreeEntry(new GUIContent("对话", indentationIcon))
                {
                    level = 1,
                    userData = NodeType.Dialogue
                },
                new SearchTreeEntry(new GUIContent("分支", indentationIcon))
                {
                    level = 1,
                    userData = NodeType.Branch
                },
                new SearchTreeEntry(new GUIContent("背景图片", indentationIcon))
                {
                    level = 1,
                    userData = NodeType.BGI
                },
                //new SearchTreeEntry(new GUIContent("背景音乐", indentationIcon))
                //{
                //    level = 1,
                //    userData = NodeType.BGM
                //},
                new SearchTreeEntry(new GUIContent("编辑变量", indentationIcon))
                {
                    level = 1,
                    userData = NodeType.EditVar
                },
                new SearchTreeEntry(new GUIContent("结束", indentationIcon))
                {
                    level = 1,
                    userData = NodeType.End
                },
                new SearchTreeEntry(new GUIContent("布局", indentationIcon))
                {
                    level = 1,
                    userData = NodeType.Layout
                },
            };
            
            return searchTreeEntries;
        }

        /// <summary>
        /// 当点击对话框某个按钮时
        /// </summary>
        /// <param name="SearchTreeEntry">点击的按钮</param>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context)
        {
            // 获取本地点击坐标
            Vector2 localMousePostion = graphViewer.GetLocalMousePosition(context.screenMousePosition, true);

            // 检测节点类型执行对应操作
            NodeType type = (NodeType)SearchTreeEntry.userData;
            switch (type)
            {
                //case Group _:
                //    graphViewer.CreateGroup("分组", localMousePostion);
                //    break;

                case NodeType.Base:
                //case NodeType.ZeroInZeroOut:
                case NodeType.ZeroInSingleOut:
                //case NodeType.ZeroInMultiOut:
                case NodeType.SingleInZeroOut:
                case NodeType.SingleInSingleOut:
                case NodeType.SingleInMultiOut:
                //case NodeType.MultiInZeroOut:
                //case NodeType.MultiInSingleOut:
                //case NodeType.MultiInMultiOut:
                case NodeType.End:
                case NodeType.Dialogue:
                case NodeType.BGI:
                //case NodeType.BGM:
                case NodeType.EditVar:
                case NodeType.Layout:
                case NodeType.Branch:
                    BaseNode node = graphViewer.CreateNode(SearchTreeEntry.content.text, type, localMousePostion);
                    //检测是否是连线
                    if (visualElement is Edge)
                    {
                        // 是则直接插入节点
                        graphViewer.InsertNodeToEdge(node, visualElement as Edge);
                    }
                    return true;

                default:
                    return false;
            }
        }
    }
}